package routines;

import processors.*;
import datastructures.*;
import java.util.Iterator;

public class Program {

	/**
	 * @uml.property  name="usedRoutines"
	 */
	private MultiCollection usedRoutines;
		
	/**
	 */
	public void addRoutine(String routine){
		usedRoutines.insert(routine);
	}

	/**
	 */
	public int getBestRuntime(RoutineCollection collection, CPU cpu){
		int bestRuntime = 0;
		Routine curRoutine;
		Iterator<String> CommandIt;
		Iterator<String> RoutineIt;
		
		RoutineIt = usedRoutines.getIterator();
		while (bestRuntime >= 0 && RoutineIt.hasNext()) {
			curRoutine = collection.getRoutine(RoutineIt.next());
			bestRuntime += curRoutine.getRuntime();
	
			CommandIt = curRoutine.getUsedCommands().getIterator();
			while (bestRuntime >= 0 && CommandIt.hasNext()) {
				if (cpu.existsCommand(CommandIt.next()) == false)
					bestRuntime = -1;
			}
		}
		
		if (bestRuntime < 0)
			return 0;
		else
			return bestRuntime;
	}



}
